home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Java for 3D & VRML Worlds
/
Java for 3d and VRML Worlds.iso
/
examples
/
chap04
/
FollowingAgent.java
< prev
next >
Wrap
Text File
|
1996-09-19
|
2KB
|
52 lines
//
// an agent is always following you.
//
import vrml.*;
import vrml.node.*;
import vrml.field.*;
public class FollowingAgent extends Script{
SFVec3f setAgentPosition;
float yourPosition[] = new float[3];
float agentPosition[] = new float[3];
// the agent's position when you are at (0, 0, 0).
final float agentDefaultPositionX = 2.0f;
final float agentDefaultPositionY = -1.4f;
final float agentDefaultPositionZ = -6.0f;
public void initialize(){
// get the reference of the event out 'setAgentPosition'.
setAgentPosition = (SFVec3f)getEventOut("setAgentPosition");
}
public void processEvent(Event e){
if(e.getName().equals("currentPosition") == true){
// record your current position.
((ConstSFVec3f)e.getValue()).getValue(yourPosition);
// move the agent.
moveAgent();
}
}
// move the agent just around you.
void moveAgent(){
// translate the agent according to your current position.
agentPosition[0] = yourPosition[0] + agentDefaultPositionX;
agentPosition[1] = yourPosition[1]+ agentDefaultPositionY;
agentPosition[2] = yourPosition[2]+ agentDefaultPositionZ;
// debug
System.out.println("moveAgent : your current position is (" +
yourPosition[0] + ", " +
yourPosition[1] + ", " +
yourPosition[2] + ")");
// move the agent to the new position.
setAgentPosition.setValue(agentPosition);
}
}